/**
 * Created by yuris on 03.01.15.
 */
package controller {
import flash.geom.Point;

import model.Constants;

import model.RuntimeProxy;
import model.objects.CellData;
import model.objects.ShopObjectData;
import model.objects.ShopperData;

import mvcexpress.mvc.PooledCommand;

public class ShopperActionCommand extends PooledCommand {
    [Inject]
    public var runtimeProxy:RuntimeProxy;

    private var _targetPoint:Point;
    private var _nextStepPoint:Point;

    private var _nearPoints:Vector.<Point>;
    private var _nearCellsForPath:Vector.<CellData>;
    private var _nearShelfs:Vector.<ShopObjectData>;


    public function execute(data:ShopperData):void {
        if(data.position.equals(data.path[data.pathPointer])){
            data.pathPointer ++;
            if(data.pathPointer >= data.path.length){
                data.isActive = false;
                return;
            }
        }
        this._targetPoint = data.path[data.pathPointer];
        this._nearPoints = runtimeProxy.selectNearPoints(data.position);
        this._nearCellsForPath = runtimeProxy.filterForWalk(data.position.clone(),this._nearPoints);

        var i:int = 0;

        //-----------------
        //watching products if shelf is near
        //------------------
        if(this._nearCellsForPath.length != this._nearPoints.length){ //we have some non free cells
            if(data.prevAction != Constants.SHOPPER_ACTION_WATCH_PRODUCT){
                this._nearShelfs = runtimeProxy.filterForShelfs(this._nearPoints);
                if(this._nearShelfs.length > 0){
                    for (i = 0; i < this._nearShelfs.length; i++) {
                        if(runtimeProxy.getShelfClientPoint(this._nearShelfs[i]).equals(data.position)){
                            if(Math.random() < 1.95){//TODO: correct random
                                data.setAction(Constants.SHOPPER_ACTION_WATCH_PRODUCT,this._nearShelfs[i]);
                                return;
                            }
                        }
                    }
                }
            }
        }

        //-----------------
        //walking
        //------------------
        i = 0;// don't go back
        var _nearCellsForPathBeforeSplice:Vector.<CellData> = this._nearCellsForPath;
        if(data.previousPosition){
            while(i < this._nearCellsForPath.length){
                if(this._nearCellsForPath[i].quadCoords.equals(data.previousPosition)){
                    this._nearCellsForPath.splice(i,1);
                    break;
                } else {
                    i++;
                }
            }
        }
        this._nextStepPoint = this._getNearestPoint(data.path[data.pathPointer], this._nearCellsForPath);
        data.setAction(Constants.SHOPPER_ACTION_GO, this._nextStepPoint.clone());

    }

    private var _bufPoint:Point;
    private var _bufDistance:int;
    private function _getNearestPoint(targetPoint:Point, availablePoints:Vector.<CellData>):Point {
        var _minDistancePointId:int = 0;
        var _minDistance:int = Point.distance(availablePoints[_minDistancePointId].quadCoords,targetPoint);

        for (var i:int = 0; i < availablePoints.length; i++) {
            _bufPoint = availablePoints[i].quadCoords;
            _bufDistance = Point.distance(targetPoint,_bufPoint);
            if(_bufDistance < _minDistance){
                _minDistance = _bufDistance;
                _minDistancePointId = i;
            }
        }

        return availablePoints[_minDistancePointId].quadCoords;
    }
}
}
